#include "Client.h"
#include "Slot.h"



Client::Client(Table* _table){
  table = _table;
  cardX=0;
  cardY=0;
  suit=0;
  number=0;
  dragX=0;
  dragY=0;
  //window being the box that we're playing in
  windowMaxX = 78;
  windowMaxY = 36;
  windowXOffset = 0;
  windowYOffset = 1;
  initializeDisplay();
}

void Client::initializeDisplay(){
  
  gameDisplay.drawBox(windowXOffset,windowYOffset, windowMaxX,windowMaxY,0);

//Iterates through and sets all possible locations into the Slot objects
  for(int i = 0; i < 8; i++){
    p1Cards[i].setX(18 + 6*i);
    p1Cards[i].setY(27);
  }
  for(int i=0; i<2; i++){
    p2Cards[i].setX(8);
    p2Cards[i].setY(12 + 5*i);
  }
  for(int i=0; i<2; i++){
    p3Cards[i].setX(32 + 6*i);
    p3Cards[i].setY(4);
  }
  for(int i=0; i<2; i++){
    p4Cards[i].setX(64);
    p4Cards[i].setY(12 + 5 *i);
  }

//Change the coordinates of the cards here if necessary
  actives[0].setX(36);
  actives[0].setY(21);
  actives[1].setX(17);
  actives[1].setY(15);
  actives[2].setX(36);
  actives[2].setY(9);
  actives[3].setX(55);
  actives[3].setY(15);
  pillage.setX(32);
  pillage.setY(15);
  spoils.setX(40);
  spoils.setY(15);
}


void Client::drawTable(Table* table){ //the called from within a loooooop
  //Creates the box to cover previous table
  gameDisplay.drawBox(windowXOffset,windowYOffset, windowMaxX,windowMaxY,0); 
  //Next line shows the Deck of rank 18
  gameDisplay.displayCard(p1Cards[0].getX(),p1Cards[0].getY(), 1, 18,0);
  //This loop gives the player1's hand
  int handSize = table->players.at(0).handCount();
  for(int i = 0; i < handSize; i++){
    int tempRank = table->players.at(0).getHand()->at(i)->getRank();
    int tempSuit = table->players.at(0).getHand()->at(i)->getSuit();
    gameDisplay.displayCard(p1Cards[i+1].getX(),p1Cards[i+1].getY(), tempSuit,tempRank,0);
  } 

  //This displays the player1's active
  gameDisplay.displayCard(p1Cards[8].getX(),p1Cards[8].getY(), 2,15,0);
  //player2's Deck and Hand
  gameDisplay.displayCard(p2Cards[0].getX(),p2Cards[0].getY(), 2,18,0);
  gameDisplay.displayCard(p2Cards[1].getX(),p2Cards[1].getY(), 2,14,0);
  //player3's Deck and Hand
  gameDisplay.displayCard(p3Cards[0].getX(),p3Cards[0].getY(), 3,18,0);
  gameDisplay.displayCard(p3Cards[1].getX(),p3Cards[1].getY(), 3,14,0);
  //player4's Deck and Hand
  gameDisplay.displayCard(p4Cards[0].getX(),p4Cards[0].getY(), 4,18,0);
  gameDisplay.displayCard(p4Cards[1].getX(),p4Cards[1].getY(), 4,14,0);
  //Active Cards
  for(int i=0; i<4; i++){
    gameDisplay.displayCard(actives[i].getX(),actives[i].getY(), 2,15,0);
  }
  //Pillage and Spoils
  gameDisplay.displayCard(pillage.getX(),pillage.getY(),1,16,0);
  gameDisplay.displayCard(spoils.getX(),pillage.getY(),1,17,0);

	//USE THIS FOR DEBUGGING - for seeing what a card value is
/* 
    int tempRank = table->players.at(0).getHand()->at(2)->getRank();
    int tempSuit = table->players.at(0).getHand()->at(2)->getSuit();
    cout<<tempRank<<endl;
    gameDisplay.displayCard(0,0, tempSuit, tempRank,0);
*/
}

Slot Client::checkHitBoxes(){

    for(int i=0; i<8; i++){
      if(between (p1Cards[i].getX(), clickX, p1Cards[i].getX()+5)){
	if(between(p1Cards[i].getY(), clickY, p1Cards[i].getY()+4)){
          return p1Cards[i];
        }
      }
    }
    for(int i=0; i<2; i++){
      if(between (p2Cards[i].getX(), clickX, p2Cards[i].getX()+5)){
	if(between(p2Cards[i].getY(), clickY, p2Cards[i].getY()+4)){
          return p2Cards[i];
        }
      }
    }
    for(int i=0; i<2; i++){
      if(between (p3Cards[i].getX(), clickX, p3Cards[i].getX()+5)){
	if(between(p3Cards[i].getY(), clickY, p3Cards[i].getY()+4)){
          return p3Cards[i];
        }
      }
    }
    for(int i=0; i<2; i++){
      if(between (p4Cards[i].getX(), clickX, p4Cards[i].getX()+5)){
	if(between(p4Cards[i].getY(), clickY, p4Cards[i].getY()+4)){
          return p4Cards[i];
        }
      }
    }

    if(between(pillage.getX(), clickX, pillage.getX()+5)){
      if(between(pillage.getY(), clickY, pillage.getX()+4)){
        return pillage;
      }
    }

    if(between(spoils.getX(), clickX, spoils.getX()+5)){
      if(between(spoils.getY(), clickY, spoils.getX()+4)){
        return spoils;
      }
    }

    for(int i=0; i<3; i++){
      if(between(actives[i].getX(), clickX, actives[i].getX()+5)){ 
        if(between(actives[i].getY(), clickY, actives[i].getX()+4)){
          return actives[i];
        }
      }
    }


  
  return emptySlot;
}

bool Client::between(int a, int b, int c){
  if((a < b) && (b < c)){
    return true;
  }
  return false;
}

void Client::setMove(Slot clickedSlot){
  if(clickedSlot == p1Cards[1]){//if you click the first hand position 
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() <= 1){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(0));//then set move to that card
    }
  }
/*
  if(clickedSlot == p1Cards[2]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() <= 2){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(1));
    }
  }
  if(clickedSlot == p1Cards[3]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() <= 3){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(2));
    }
  }
  if(clickedSlot == p1Cards[4]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() < 4){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(3));
    }
  }
  if(clickedSlot == p1Cards[5]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() < 5){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(4));
    }
  }
  if(clickedSlot == p1Cards[6]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() < 6){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(5));
    }
  }
  if(clickedSlot == p1Cards[7]){//if you click the first hand position then set move to that card
    if(clickedSlot.getCard()->getOwner()->getMaxHandSize() < 7){//but only if they actually have 4 cards in hand
    clickedSlot.getCard()->getOwner()->setMoveCard(clickedSlot.getCard()->getOwner()->getHand()->at(6));
    }
  }
*/
}

void Client::updateClient(Table* table){
  drawTable(table); 
  // calls the game display to capture some input
  gameDisplay.updateScreen();
  // calls the game display to capture some input
  key = gameDisplay.captureInput();
  Slot clickedSlot = checkHitBoxes();
  setMove(clickedSlot);//this sets a player's move based on which slot was clicked
 
      // if a mouse event occurred
      if (key == -1) {
        // make a banner message
        messageString.str("");
        messageString << "A mouse event occurred x=" \
          << gameDisplay.getMouseEventX() << ", y=" \
          << gameDisplay.getMouseEventY() << ", bstate=" \
          << gameDisplay.getMouseEventButton();
        // display a banner message
        gameDisplay.bannerTop(messageString.str());
        // record the location of the mouse event
        clickX = gameDisplay.getMouseEventX();
        clickY = gameDisplay.getMouseEventY();
        // Some of the mouse click values are defined in display.h
        // check if it was a left click
        if (gameDisplay.getMouseEventButton()&LEFT_CLICK) {
	  int num = checkHitBoxes().getX();
	  string result;
	  ostringstream convert;
	  convert <<num;
	  result = convert.str();
          gameDisplay.bannerBottom(result);
	}
        // check if it was a right click
        else if (gameDisplay.getMouseEventButton()&RIGHT_CLICK) {
        }
     
    }
  
  
}


